package health.lm.com.shop.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxSPTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.callback.Callback;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.data.AppModel;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLAllCompanyAdapter;
import health.lm.com.shop.entity.HLAllCompanyEntity;
import health.lm.com.shop.presenter.HLAllCompanyPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/13 14:07
 * Introduce：全部商会/影响力中心
 */
@Route(path = ARouterConstant.HLAllCompanyActivity)
public class HLAllCompanyActivity extends XListActivity<HLAllCompanyPresenter> {
    public static final int TYPE_BUSINESS = 1;
    public static final int TYPE_INFLUENCE = 2;
    @Autowired
    public int type;
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.rv_news)
    RecyclerView mRvNews;
    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    private HLAllCompanyAdapter mAllCompanyAdapter;

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        switch (type) {
            case TYPE_BUSINESS:
                getP().getBusinessListData(page, pageSize);
                break;
            case TYPE_INFLUENCE:
                getP().getInfluenceCenterData(page, pageSize);
                break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_bar_refresh_list;
    }

    @Override
    public HLAllCompanyPresenter newP() {
        return new HLAllCompanyPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.recyclerView = mRvNews;
        super.rlRefreshLayout = mSmartRefresh;
        mAllCompanyAdapter = new HLAllCompanyAdapter(new ArrayList<>());
        super.adapter = mAllCompanyAdapter;
        super.initData(savedInstanceState);
        mTitleBar.setListener((v, action, extra) -> finish());
        if (type == TYPE_BUSINESS) mTitleBar.getCenterTextView().setText("全部商会");
        else if (type == TYPE_INFLUENCE) mTitleBar.getCenterTextView().setText("影响力中心");
        mRvNews.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)));
        mRvNews.setLayoutManager(new LinearLayoutManager(context));
        mRvNews.setAdapter(mAllCompanyAdapter);

        mAllCompanyAdapter.setOnItemClickListener((adapter1, view, position) -> {
            HLAllCompanyEntity entity = (HLAllCompanyEntity) adapter1.getData().get(position);
            ARouter.getInstance().build(ARouterConstant.HLMyCompanyActivity)
                    .withString("id", entity.getId())
                    .withString("title", "我的商会")
                    .navigation();
        });
        mAllCompanyAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            HLAllCompanyEntity entity = (HLAllCompanyEntity) adapter1.getData().get(position);
            if (view.getId() == R.id.textView47) {
                if (entity.getAttention().equals("1")) return;
                getP().attention(entity.getId(), s -> {
                    String attention = entity.getAttention().equals("1") ? "0" : "1";
                    entity.setAttention(attention);
                    adapter1.notifyDataSetChanged();
                });
            }
        });

        if (type == TYPE_BUSINESS) getP().getBusinessListData(page, pageSize);
        if (type == TYPE_INFLUENCE) getP().getInfluenceCenterData(page, pageSize);
    }

    public void setNewData(List<HLAllCompanyEntity> pNewData) {
        if (isRefresh) {
            mAllCompanyAdapter.setNewData(pNewData);
            mSmartRefresh.finishRefresh();
        } else {
            mAllCompanyAdapter.addData(pNewData);
            if (pNewData.size() >= pageSize) mAllCompanyAdapter.loadMoreComplete();
            else mAllCompanyAdapter.loadMoreEnd();
        }
        if(isRefresh && pNewData.size() >= pageSize) initLoadMore();
        if (mAllCompanyAdapter.getData().size() <= 0) mAllCompanyAdapter.setEmptyView(empty());
    }
}
